package org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd;

import org.wu.framework.lazy.orm.core.config.ReverseEngineering;
import org.wu.framework.lazy.orm.core.config.enums.OrmArchitecture;
import org.wu.framework.lazy.orm.core.persistence.reverse.ReverseClassLazyTableEndpoint;
import org.wu.framework.lazy.orm.core.persistence.reverse.lazy.similar.DefaultAbstractJavaReverseEngineeringMapperMethod;
import org.wu.framework.core.NormalUsedString;

import java.io.File;

/**
 * description
 *
 * @author 吴佳伟
 * @date 2023/02/14 14:37
 */
public  class DefaultDDDLazyInfrastructureMapper extends DefaultAbstractJavaReverseEngineeringMapperMethod {


    public DefaultDDDLazyInfrastructureMapper(ReverseClassLazyTableEndpoint reverseClassLazyTableEndpoint, ReverseEngineering reverseEngineering) {
        super(reverseClassLazyTableEndpoint, reverseEngineering);
    }

    @Override
    public void initImportClassName() {

        ReverseClassLazyTableEndpoint tableEndpoint = this.getReverseClassLazyTableEndpoint();
        if (OrmArchitecture.MYBATIS.equals(this.getOrmArchitecture())
        ) {
            super.initImportClassName();
            addImportClassName("com.baomidou.mybatisplus.core.mapper.BaseMapper");
            addImportClassName("org.apache.ibatis.annotations.Mapper");
            addImportClassName(tableEndpoint.getPackageName() + NormalUsedString.DOT + "infrastructure.entity" + NormalUsedString.DOT + tableEndpoint.getClassName() + "DO");
        }
    }

    /**
     * 获取 模型 class 并且将第一个字母小写
     *
     * @return 模型 class 并且将第一个字母小写
     */
    @Override
    public String getModelClassLowercaseFirstLetter() {
        ReverseClassLazyTableEndpoint tableEndpoint = this.getReverseClassLazyTableEndpoint();
        return tableEndpoint.getClass2LowName() + "DO";
    }

    /**
     * 获取 模型class
     *
     * @return 模型class
     */
    @Override
    public String getModelClassName() {
        ReverseClassLazyTableEndpoint tableEndpoint = this.getReverseClassLazyTableEndpoint();
        return tableEndpoint.getClassName() + "DO";
    }

    /**
     * 获取 模型class 完整地址
     *
     * @return 模型class 完整地址
     */
    @Override
    public String getModelClass() {
        ReverseClassLazyTableEndpoint tableEndpoint = this.getReverseClassLazyTableEndpoint();
        return tableEndpoint.getPackageName() + NormalUsedString.DOT + "infrastructure.entity" + NormalUsedString.DOT + tableEndpoint.getClassName() + "DO";
    }

    /**
     * description:  添加 class 上的注解
     * class 上的注解如  @LazyTableFieldId(name = "%s", comment = "%s")
     *
     * @author 吴佳伟
     * @date: 13.2.23 17:13
     */
    @Override
    public void initClassAnnotationPart() {

        if (OrmArchitecture.MYBATIS.equals(this.getOrmArchitecture())) {
            super.initClassAnnotationPart();
        }

    }

    /**
     * description:  添加 class 字段片段
     *
     * <p>@LazyTableFieldId(comment = "api参数主键")</p>
     * private Long id;
     * </p>
     *
     * @author 吴佳伟
     * @date: 13.2.23 17:13
     */
    @Override
    public void initClassFieldPart() {

    }

    /**
     * description:  添加 class
     *
     * @author 吴佳伟
     * @date: 13.2.23 17:13
     */
    @Override
    public void initClassNamePart() {

        ReverseClassLazyTableEndpoint tableEndpoint = this.getReverseClassLazyTableEndpoint();
        String mapperName = String.format("%sMapper", tableEndpoint.getClassName());
        setFileName(mapperName);

        ReverseEngineering reverseEngineering = this.getReverseEngineering();
        if (OrmArchitecture.MYBATIS.equals(reverseEngineering.getOrmArchitecture())) {
            super.initClassNamePart();
            addClassNamePart(String.format("public interface %s extends BaseMapper<%sDO> {" + NormalUsedString.NEWLINE, mapperName, tableEndpoint.getClassName()) + NormalUsedString.NEWLINE);
        } else {
            addClassNamePart(String.format("public interface %s {" + NormalUsedString.NEWLINE, mapperName) + NormalUsedString.NEWLINE);
        }


    }

    @Override
    protected String getPackage() {
        ReverseClassLazyTableEndpoint tableEndpoint = this.getReverseClassLazyTableEndpoint();
        return String.format("package %s;", tableEndpoint.getPackageName() + NormalUsedString.DOT + "infrastructure" + NormalUsedString.DOT + "mapper") + NormalUsedString.NEWLINE + NormalUsedString.NEWLINE;
    }

    /**
     * description:  添加 class 方法片段
     *
     * @author 吴佳伟
     * @date: 13.2.23 17:13
     */
    @Override
    public void initClassMethodPart() {
        if (OrmArchitecture.MYBATIS.equals(this.getOrmArchitecture())) {
            super.initClassMethodPart();
        }
    }

    /**
     * 获取 文件module type
     *
     * @return
     */
    @Override
    protected String getModuleType() {
        return "infrastructure" + File.separator + "mapper";
    }

}
